package Verifier.ArithmeticSystem;

import Verifier.ByteTree.ByteTree;

public class ArithmeticConvertorIntBoolTester {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		testBooleanArray(new boolean[] { true, false, false, true }, "010000000401000001");
		System.out.println();
		testBooleanArray(new boolean[] { false }, "010000000100");
		System.out.println();
		testBooleanArray(new boolean[] { true, false, false, true, true, false, true, true, true, false }, "010000000A01000001010001010100");
		System.out.println();
		testInt(263, "01000000020107");
		System.out.println();
		testInt(-263, "0100000002FEF9");
		System.out.println();
		testInt(65536, "010000000400010000");
	}
	
	private static void printBooleanArray(boolean[] elements) {
		System.out.print("[");
		for (int i = 0 ; i < elements.length; i++) {
			System.out.print(elements[i]);
			if (i + 1 < elements.length) {
				System.out.print(",");
			}
		}
		System.out.print("]");
	}
	
	private static boolean testBooleanArray(boolean[] resultArray, String treeString) {
		System.out.println("Test : Boolean Array");
		System.out.println("--------------------");
		// convert the byte tree into a boolean array
		System.out.print("Create Boolean Array of ");
		printBooleanArray(resultArray);
		System.out.println();
		boolean[] bools = null;
		try {
			bools = ArithmeticConvertor.ToBooleanArray(new ByteTree(treeString));
			for (int i = 0; i < bools.length; i++) {
				if (bools[i] != resultArray[i]) {
					System.out.println("Failed : the element no." + i + " created is different from the result");
					return (false);
				}				
			}
			System.out.println("OK : Boolean Array created");
		} catch (Exception e) {
			System.out.println("Failed : creating the Boolean Array");
			return (false);
		}
		
		System.out.print("The Boolean Array created is: ");
		printBooleanArray(bools);
		System.out.println();
		
		// convert the boolean array into a byte tree
		System.out.println("Converting the Boolean Array backwards:");
		try {
			ByteTree b = ArithmeticConvertor.ToByteTree(bools);
			
			if (0 == b.getByteString().compareToIgnoreCase(treeString)) {
				System.out.println("OK : The created byte tree is the same as the one that created the array");
			} else {
				System.out.println("Failed : The created byte tree is the different as the one that created the group");
				System.out.println(b.getByteString());
				System.out.println(treeString);
			}
		} catch (Exception e) {
			System.out.println("Failed to create the byte tree");
			return (false);
		}
		
		return (true);
	}
	
	private static boolean testInt(int resultElement, String treeString) {
		System.out.println("Test : Int");
		System.out.println("----------------------------");
		// convert the byte tree into an int
		System.out.println("Create Int : n = " + resultElement);
		int e = 0;
		try {
			e = ArithmeticConvertor.ToInt(new ByteTree(treeString));
			if (resultElement != e) {
				System.out.println("Failed : the int created is different from the result");
				return (false);
			}
			System.out.println("OK : int created");
		} catch (Exception ex) {
			System.out.println("Failed : creating the int");
			return (false);
		}
		
		System.out.println("The Int created is: " + e);
		
		// convert the Int into a byte tree
		System.out.println("Converting the Int backwards:");
		try {
			ByteTree b = ArithmeticConvertor.ToByteTree(e);
			
			if (0 == b.getByteString().compareToIgnoreCase(treeString)) {
				System.out.println("OK : The created byte tree is the same as the one that created the element");
			} else {
				System.out.println("Failed : The created byte tree is the different as the one that created the element");
				System.out.println(b.getByteString());
				System.out.println(treeString);
			}
		} catch (Exception ex) {
			System.out.println("Failed to create the byte tree");
			return (false);
		}
		
		return (true);
	}
}
